﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Threading;

namespace Shop_Inventory_Management.Controller
{
    class CoordinatingController
    {
        private static CoordinatingController CoordinatingControllerInstance;
        private SynchronManager SynController;
        private SecurityManager SecController;
        
        private string mErrorMsg;

        private CoordinatingController()
        {
            mErrorMsg = string.Empty;
            SecController = SecurityManager.getInstance();
            SynController = SynchronManager.getInstance();
        }

        public static CoordinatingController getInstance()
        {
            if (CoordinatingControllerInstance == null)
            {
                CoordinatingControllerInstance = new CoordinatingController();
            }
            return CoordinatingControllerInstance;
        }

        public bool StartOperation(CommandMode operation)
        {
            try
            {
                switch (operation)
                {
                    case CommandMode.Synchron:
                        SynController.SynchronizeTables();
                        break;
                    case CommandMode.Update:
                        SynController.UpdateStockPrice();
                        break;
                }
                return true;
            }
            catch (Exception e)
            {
                RecordError(e);
                sendEmail();
                return false;
            }
        }

        private void sendEmail()
        {
            try
            {
                DateTime now = DateTime.Now;
                string htmlBody = Constant.EMAIL_BODY_SYN;
                var SmtpServer = new SmtpClient(Constant.EMAIL_HOST, Constant.EMAIL_PORT)
                {
                    Credentials = new NetworkCredential(SecController.Decrypt(Constant.EMAIL_SENDER),
                                                    SecController.Decrypt(Constant.EMAIL_SENDER_PASSWORD)),
                    EnableSsl = true,

                };
                var mail = new MailMessage()
                {
                    From = new MailAddress(SecController.Decrypt(Constant.EMAIL_SENDER)),
                    Subject = Constant.EMAIL_SUBJECT,
                    IsBodyHtml = true,
                    Body = htmlBody,
                };
                mail.To.Add(Constant.EMAIL_RECEIVER);
                SmtpServer.Send(mail);
            }
            catch (Exception e)
            {
                RecordError(e);
            }
        }

        private void RecordError(Exception e)
        {
            mErrorMsg = e.ToString();
            DateTime now = DateTime.Now;
            ErrorLog.Log(now.ToString());
            ErrorLog.Log(mErrorMsg);
        }
    }
}
